package com.udesc.larva.delta.ui;

import java.awt.*;
import java.awt.event.*;

import com.udesc.larva.delta.ui.element.Curvas;

public class MyCanvas extends Canvas
{
    Interface inte;
	Curvas curvas;
    int x = 0;
    int y = 0;
    int z = 0;

	public MyCanvas(Interface inte)
	{
	    this.inte = inte;
        BCML bcmlistener = new BCML();
		addMouseListener(bcmlistener);
		addMouseMotionListener(bcmlistener);
		setBackground(Color.white);
		curvas = new Curvas();
	}

	public void paint(Graphics g)
	{
        Dimension size = getSize();
	    g.clearRect(0, 0, size.width, size.height);
		drawBorder(g);
		curvas.paint(g, inte);
        if (x==1)
	    {
            curvas.fecha(g);
	        x=0;

        }
        if(y==1)
        {
          curvas.salvaPlano();
	      curvas.limpaPlano(g);
          y=0;
          repaint();
        }
    	if (z==1)
	    {
            curvas.limpaPlano(g);
	        z=0;
	        repaint();
        }

    }
	public void drawBorder(Graphics g)
	{	Dimension size = getSize();
		g.drawRect(0, 0, size.width - 1, size.height - 1);
	}

	public Dimension getPreferredSize()
		{ return new Dimension(620, 360); }

	public void togglePoints()
	{	curvas.togglePoints();
		repaint();
	}

    public void novo()
	{	curvas.novo();
		repaint();
	}
	public void delete()
	{	curvas.delPt();
		repaint();
	}
    public void fecha()
	{  x=1;
       repaint();
    }
    public void salvaPlano()
	{
       y=1;
       repaint();
    }
    public void limpaPlano()
	{
       z=1;
       repaint();
    }
    public void salvar()
	{
       curvas.salvar();
       repaint();
    }

	// Eventos do mouse
	class BCML extends MouseAdapter implements MouseMotionListener
	{   public void mousePressed(MouseEvent e)
		{	curvas.setCurrPoint(e.getPoint());
			repaint();
		}
		public void mouseDragged(MouseEvent e)
		{	curvas.moveCurrPtTo(e.getPoint());
			repaint();
		}
		public void mouseMoved(MouseEvent e)
		{ }
	}
}